<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * share_model.php
 * 分享模型
 * @author  :  Skiychan <developer@zzzzy.com>
 * @created :  2015/4/14
 * @modified:  
 * @version :  0.0.1
 */
class Share_model extends MY_Model {
    //table names

    public function __construct() {
        parent::__construct();
    }

    public function get_insert_id(){
        return $this->db->insert_id();
    }

    /**
     * 查询是否已存在分享
     * @param array $where 条件
     * @param string $fields 返回的字段信息
     * @return mixed
     */
    public function check_code_exists($where = array(), $fields = "id, share_code, password") {
        return $this->db->select($fields)->limit(1)->get_where(self::TB_SHARE_LINK, $where)->row_array();
    }

    /**
     * 取得文件或者文件夹名
     * @param $id 编号
     * @param $type 类型 1为文件，2为文件夹
     * return array
     */
    public function get_share_name($id, $type) {
        if (empty($id) || empty($type)) return NULL;
        $tb = $type == 1 ? self::TB_FILE : self::TB_FOLDER;
        $test = $this->db->select('name')->get_where($tb, array('id' => $id), 1)->row_array();
        return $test;
    }

    /**
     * 获取接收者
     * @param $where 条件
     * @return mixed
     */
    public function get_share_receivers($where) {
        if (empty($where))  return NULL;
        return $this->db->select('receiver')->get_where(self::TB_SHARE_RECEIVERS, $where, 1)->row_array();
    }

    /**
     * 通过一组文件夹查询文件 access_denied = 0, deleted = 0
     * @param $uid
     * @param $fids 文件夹编号
     * @param string $file_fields 查询的文件字段
     * @return null
     */
    public function get_folder_datas($uid, $fids, $file_fields="id,name,folder_id,ext_name", $secure_fields="") {
        if(empty($uid) || (empty($fids))) return NULL;

        //u_files 最终组合成 f.id,f.name,f.folder_id,f.ext_name形式
        $new_fields = $this->assemble_field($file_fields, "f");

        //guid表
        if (! empty($secure_fields)) {
            $this->db->join(self::TB_SECURE_FILES." s", "f.id = s.fid", "LEFT");
            $new_fields = $new_fields.",".$this->assemble_field($secure_fields, "s");
        }
        $where['f.uid'] = $uid;
        $where['f.access_denied'] = 0;
        $where['f.deleted'] = 0;
        if (is_array($fids)) {
            if (count($fids) > 1) {
                $this->db->where_in("f.folder_id", $fids);
            } else {
                $where["f.folder_id"] = $fids[0];
                $this->db->limit(1);
            }
        } else {
            $where["f.folder_id"] = $fids;
            $this->db->limit(1);
        }

        $this->db->where($where);
        $this->db->select($new_fields);
        $this->db->order_by('f.folder_id', "asc");
        $query = $this->db->from(self::TB_FILE." f")->get();

        return $query->result_array();
    }

    /**
     * 查询一组文件信息 access_denied = 0, deleted = 0
     * @param $uid
     * @param $fids 文件列表id
     * @param string $where 查询条件
     * @return null
     */
    public function get_file_list($uid, $fids, $where="") {
        if (empty($uid) || empty($fids))  return NULL;

        if (! empty($where)) {
            $this->db->where($where);
        }
        $df_where = array(
            'uid' => $uid,
            'access_denied' => 0,
            'deleted' => 0
        );
        $this->db->where($df_where);
        if (! is_array($fids)) $fids = (array)$fids;
        $this->db->where_in('id', $fids);
        return $this->db->get(self::TB_FILE)->result_array();
    }

    /**
     * 获取一组文件夹信息
     * @param $uid
     * @param $fids 文件夹id列表
     * @param string $where 条件
     * @param string $fields 查询字段
     * @return null
     */
    public function get_folders_info($uid, $fids, $where="", $fields="id,name,pid,path,deleted,access_denied") {
        if (empty($uid) || empty($fids))  return NULL;

        //SELECT id,name,pid,path,deleted,access_denied FROM u_folders WHERE id in (166,167)
        $this->db->where('uid', $uid);
        $this->db->where_in('id', $fids);
        //条件不为空
        if (! empty($where)) {
            $this->db->where($where);
        }
        $this->db->select($fields);
        return $this->db->get(self::TB_FOLDER)->result_array();
    }

    /**
     * 添加分享
     * @param $data 更新的值
     * @return bool
     */
    public function add_share($data) {
        if (empty($data)) return NULL;
        return $this->db->insert(self::TB_SHARE_LINK, $data);
    }

    /**
     * 更新单条分享的信息
     * @param $data 更新的值
     * @param $where 更新的条件
     * @return bool
     */
    public function update_share($where, $data) {
        if (!is_array($where) || !is_array($data)) return NULL;
        if (empty($where) || empty($data)) return NULL;

        return $this->db->update(self::TB_SHARE_LINK, $data, $where, 1);
    }

    /**
     * 批量更新下载的标志
     * @param $uid
     * @param $fids 一组文件列表
     * @param int $switch 值 0/1
     * @return null
     */
    public function update_downloaded_batch($uid, $fids) {
        if (empty($uid) || empty($fids)) return;

        if (! is_array($fids)) $fids = explode(',',$fids);
        if (count($fids) == 1) {
            $this->db->where('id', $fids[0]);
            $this->db->limit(1);
        } else {
            $this->db->where_in('id', $fids);
        }
        $this->db->where('uid', $uid);

        return $this->db->update(self::TB_FILE, array('downloaded' => 1));
    }

    /**
     * 更新 分享 的开关
     * @param $uid
     * @param $fids 文件列表
     * @param int $switch 0/1 开关
     * @return null
     */
    public function update_shared_batch($uid, $fids, $switch=1) {
        if (empty($uid) || empty($fids)) return NULL;

        if (! is_array($fids)) $fids = (array)$fids;
        if ($switch != 1) $switch = 0;
        if (count($fids) == 1) {
            $this->db->where('id', $fids[0]);
            $this->db->limit(1);
        } else {
            $this->db->where_in('id', $fids);
        }
        $this->db->where('uid', $uid);

        return $this->db->update(self::TB_FILE, array('shared' => $switch));
    }

    /**
     * 获取文件夹下面的文件
     * @param $uid 用户id
     * @param $ids 文件夹的id，多个文件夹则传array(id);
     * @param bool $all 是否查找所有子目录的文件
     * @param string $file_fields 文件的字段
     * @param string $policy_fields 文件夹的字段
     * @return array|null
     */
    public function update_files_shared($uid, $ids, $switch=1)
    {
        if(empty($uid) || empty($ids)) return NULL;

        if(!is_array($ids)) {
            $where = ' AND fd1.uid = '.$this->db->escape($uid).' AND fd1.id = '.$this->db->escape($ids).' ';
        } else if(is_array($ids) && count($ids)==1) {
            $where = ' AND fd1.uid = '.$this->db->escape($uid).' AND fd1.id = '.$this->db->escape($ids[0]).' ';
        } else {
            $where = ' AND fd1.uid = '.$this->db->escape($uid).' AND fd1.id in ('.implode(",",$ids).') ';
        }

        $sql = 'UPDATE `'.self::TB_FILE.'` f
            SET f.shared = '.$switch.'
            WHERE f.folder_id
            IN (
                SELECT fd2.id
                FROM
                    `'.self::TB_FOLDER.'` fd1
                LEFT JOIN `'.self::TB_FOLDER.'` fd2 ON fd2.uid = fd1.uid
                AND fd2.deleted = fd1.deleted
                AND concat(fd2.path, ",", fd2.id) LIKE concat(fd1.path, ",", fd1.id, "%")
                WHERE fd1.uid = f.uid
                '.$where.'
            ) ';
        $this->db->query($sql);
        return $this->db->affected_rows();
    }

    /**
     * 插入值到share_list表
     * @param $data 更新的值
     * @return CI_DB_active_record
     */
    public function insert_share_list($data) {
        if (!is_array($data)) return;
        return $this->db->insert_batch(self::TB_SHARE_LIST, $data);
    }

    /**
     * 添加接收者的信息
     * @param $data 更新的值
     * @return bool|CI_DB_active_record
     */
    public function insert_receivers($data) {
        if (!is_array($data)) return;
        return $this->db->insert(self::TB_SHARE_RECEIVERS, $data);
    }

    /**
     * 更新接收者的信息
     * @param $data 更新的值
     * @param $where 更新分享条件
     * @return bool|CI_DB_active_record
     */
    public function update_receivers($where, $data) {
        if (empty($where) || empty($data)) return NULL;
        return $this->db->where($where)->update(self::TB_SHARE_RECEIVERS, $data);
    }

    /**
     * Mysql组合字段
     * @param $fields 字段数组或者,分隔的字符串
     * @param $prefix 前缀
     * @return array|string
     */
    public function assemble_field($fields, $prefix) {
        //组合查询file的字段
        if (! is_array($fields)) {
            $fields = explode(",", $fields);
        }
        $fields_arr = array();
        foreach ($fields as $keys => $values) {
            $fields_arr[] = $prefix.".".$values;
        }
        $fields = implode(",", $fields_arr);  //最终组合成 f.id,f.name,f.folder_id,f.ext_name形式
        return $fields;
    }

    /**
     * 通过分享id获取文件
     * @param $lid 分享码id
     */
    public function get_files_lid($lid) {
        if (empty($lid)) {
            return;
        }

        return $this->db->get_where(self::TB_SHARE_LIST, array('lid' => $lid))->result_array();
    }

    /**
     * 删除分享的文件
     * @param $lid 分享码id
     */
    public function delete_files_lid($lid) {
        if (empty($lid)) {
            return;
        }

        return $this->db->delete(self::TB_SHARE_LIST, array('lid' => $lid));
    }
}
 
/* End of file share_model.php */
/* Location:  */